import { ref, reactive } from 'vue'
import { TASK_QUESTION_LIST_STATUS, TASK_QUESTION_TYPE } from '@/utils/dictionary'
import { getDeptTreeById } from '@/api/org-manage/depart-manage/index'
import { getTaskInspectionType } from '@/api/task/index'

// 部门列表
const deptList = ref([])

// 问题类型列表
const typeList = ref([])

const deptIdValue = ref('')

export const search = reactive({
  search: [
    {
      label: '所属任务',
      key: 'key',
      props: {
        placeholder: '请输入所属任务'
      }
    },
    {
      label: '问题核实时间',
      type: 'date',
      key: 'questionTime',
      props: {
        type: 'datetimerange',
        format: 'yyyy-MM-dd HH:mm:ss'
      }
    },
    {
      label: '问题类型',
      type: 'select',
      key: 'questionCode',
      props: {
        placeholder: '请选择问题类型',
        options: typeList
      }
    },
    {
      label: '问题状态',
      type: 'select',
      key: 'status',
      props: {
        placeholder: '请选择问题状态',
        options: TASK_QUESTION_LIST_STATUS
      }
    },
    {
      label: '所属部门',
      type: 'tree-select',
      key: 'deptId',
      value: deptIdValue,
      props: {
        placeholder: '请选择所属部门',
        options: deptList,
        labelField: 'name',
        keyField: 'id'
      }
    }
  ]
})

// 获取部门列表
export const getDeptInfo = async function(id) {
  const { data } = await getDeptTreeById(id)
  deptList.value = data?.map(it => ({
    ...it,
    label: it.name,
    value: it.id
  }))
}
// 获取问题类型列表
export const getTypeList = async function() {
  const { data } = await getTaskInspectionType()
  typeList.value = data.map(it => ({
    ...it,
    label: it.content,
    value: it.code
  }))
}

export const getDeptId = (deptId) => {
  deptIdValue.value = deptId
}
